package com.woniuxy.house.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.entity.ResponseResult;
import com.woniuxy.entity.house.SecondhouseComment;
import com.woniuxy.house.service.SecondhouseCommentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liangyu
 * @since 2021-03-19
 */
@RestController
@RequestMapping("/house/secondhouseComment")
public class SecondhouseCommentController {

    @Resource
    private SecondhouseCommentService secondhouseCommentService;

    /**
     * 新增评论
     * @param secondhouseComment
     * @return
     */
    @PostMapping("/add")
    public ResponseResult<Void> add(SecondhouseComment secondhouseComment){
        return secondhouseCommentService.save(secondhouseComment) ?
                new ResponseResult<>(500,"新增评论成功！") :
                new ResponseResult<>(200,"新增评论失败！");
    }

    /**
     * 根据二手房id查询所有评论
     * @param sid
     * @return
     */
    @GetMapping("/findBySid/{sid}/{current}/{size}")
    public ResponseResult<Page<SecondhouseComment>> findCommentsBySId(@PathVariable int sid,@PathVariable int current,@PathVariable int size){
        Page<SecondhouseComment> list = secondhouseCommentService.findCommentsBySId(sid, current, size);
        return list != null && list.getRecords().size() != 0 ?
                new ResponseResult<>(list):
                new ResponseResult<>(500,"未查询到相关评论");
    }

    /**
     * 根据经纪人id查询所有评论
     * @param bid
     * @return
     */
    @GetMapping("/findByBid/{bid}/{current}/{size}")
    public ResponseResult<Page<SecondhouseComment>> findCommentsByBId(@PathVariable int bid,@PathVariable int current,@PathVariable int size){
        Page<SecondhouseComment> list = secondhouseCommentService.findCommentsByBId(bid, current, size);
        return list != null && list.getRecords().size() != 0 ?
                new ResponseResult<>(list):
                new ResponseResult<>(500,"未查询到相关评论");
    }

    /**
     * 根据id软删除评论
     */
    @PostMapping("/{id}")
    public ResponseResult<Void> deleteOneById(@PathVariable int id) {
        return secondhouseCommentService.deleteOneById(id) ? new ResponseResult<>(200,"删除成功！")
                : new ResponseResult<>(500,"删除失败！");
    }
}

